/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import defaultAvatar from "@/assets/images/defaultAvatar.jpg";
import { ElTag } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { deleteByIds } from "$/attractions/inspection/api/inspectionRecords.ts";
import { pageAll as getPlansList } from "$/attractions/inspection/api/inspectionPlans.ts";
import useDrawer from "@/hooks/useDrawer";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any,
  drawer?: any
): MaProTableColumns[] {
  const msg = useMessage();

  const plansList = ref<any[]>([]);
  onMounted(async () => {
    // 获取供应商列表
    const plansResponse = await getPlansList({});
    if (
      plansResponse.code === 200 &&
      plansResponse.data &&
      plansResponse.data.list
    ) {
      plansList.value = plansResponse.data.list;
    }
  });

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    {
      label: "巡检计划",
      prop: "plan_id",
      renderProps: { multiple: false },
      cellRender: ({ row }) => {
        const planItem = plansList.value.find(
          (item) => item.id === row.plan_id
        );
        return planItem ? planItem.name : `未知巡检计划(${row.plan_id})`;
      },
    },
    { label: "巡检开始时间", prop: "start_time", mode: "date", showTime: true },
    { label: "巡检结束时间", prop: "end_time", mode: "date", showTime: true },
    { label: "巡检人", prop: "inspector" },
    {
      type: "operation",
      label: () => t("crud.operation"),
      operationConfigure: {
        actions: [
          {
            name: "detail",
            icon: "ep:view",
            text: () => "详情",
            onClick: ({ row }) => {
              if (drawer) {
                drawer.setTitle("巡检记录详情");
                drawer.open({ 
                  width: 800,
                  data: { inspectionRecordId: row.id }
                });
              }
            },
          },
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) =>
              hasAuth("inspection_records:inspectionRecords:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) =>
              hasAuth("inspection_records:inspectionRecords:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
